package org.zoomdy.gdbserver.extender;

public class Logger extends Node
{
    public Logger()
    {
        id = "";
    }
    
    public boolean setProperty(String name, String value)
    {
        if(name.equals("id"))
        {
            id = value;
            return true;
        }
        
        return false;
    }
    
    public String getProperty(String name)
    {
        if(name.equals("id"))
        {
            return id;
        }
        
        return null;
    }
    
    public boolean inputCommand(Packet pkt)
    {
        printPacket("->", pkt);
        
        return super.outputCommand(pkt);
    }
    
    public boolean inputResponse(Packet pkt)
    {
        printPacket("<-", pkt);
        
        return super.outputResponse(pkt);
    }
    
    private synchronized void printPacket(String prefix, Packet pkt)
    {
        if(pkt.startsWith("X"))
        {
            byte[] raw = pkt.getRaw();
            int i;
            for(i = 0; i < raw.length; i++)
            {
                if(raw[i] == ':')
                {
                    break;
                }
            }
            System.out.print(this.id);
            System.out.print(prefix);
            System.out.print(new String(raw, 0, i + 1));
            System.out.println("***BinaryData***");
        }
        else
        {
            System.out.print(this.id);
            System.out.print(prefix);
            System.out.println(pkt.toRawString());
        }
    }
    
    private String id;
}
